package com.aihna.system.service.impl;

import com.aihna.common.core.utils.MapstructUtils;
import com.aihna.common.mybatis.core.page.TableDataInfo;
import com.aihna.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.aihna.system.domain.bo.SysWechatConfigBo;
import com.aihna.system.domain.vo.SysWechatConfigVo;
import com.aihna.system.domain.SysWechatConfig;
import com.aihna.system.mapper.SysWechatConfigMapper;
import com.aihna.system.service.ISysWechatConfigService;
import com.aihna.common.core.utils.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 微信配置Service业务层处理
 *
 * @author Joseph.Chen
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
public class SysWechatConfigServiceImpl implements ISysWechatConfigService {

    private final SysWechatConfigMapper baseMapper;

    /**
     * 查询微信配置
     */
    @Override
    public SysWechatConfigVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询微信配置列表
     */
    @Override
    public TableDataInfo<SysWechatConfigVo> queryPageList(SysWechatConfigBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<SysWechatConfig> lqw = buildQueryWrapper(bo);
        Page<SysWechatConfigVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询微信配置列表
     */
    @Override
    public List<SysWechatConfigVo> queryList(SysWechatConfigBo bo) {
        LambdaQueryWrapper<SysWechatConfig> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<SysWechatConfig> buildQueryWrapper(SysWechatConfigBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<SysWechatConfig> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), SysWechatConfig::getName, bo.getName());
        lqw.eq(StringUtils.isNotBlank(bo.getAppid()), SysWechatConfig::getAppid, bo.getAppid());
        lqw.eq(bo.getType() != null, SysWechatConfig::getType, bo.getType());
        lqw.eq(bo.getStatus() != null, SysWechatConfig::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增微信配置
     */
    @Override
    public Boolean insertByBo(SysWechatConfigBo bo) {
        SysWechatConfig add = MapstructUtils.convert(bo, SysWechatConfig.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改微信配置
     */
    @Override
    public Boolean updateByBo(SysWechatConfigBo bo) {
        SysWechatConfig update = MapstructUtils.convert(bo, SysWechatConfig.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(SysWechatConfig entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除微信配置
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<SysWechatConfigVo> queryAll() {
        LambdaQueryWrapper<SysWechatConfig> lqw = Wrappers.lambdaQuery();
        lqw.eq(SysWechatConfig::getStatus, 1);
        return baseMapper.selectVoList(lqw);
    }
}
